package array;

/**
 * @author 十八岁讨厌编程
 * @date 2025/6/27 20:59
 * @PROJECT_NAME leetcode
 * @description
 */

public class Solution704 {

    public int search(int[] nums, int target) {
        int head = 0;
        int end = nums.length - 1;
        int middle = (head + end) / 2;
        while (head <= end) {
            if (nums[middle] < target) {
                head = middle + 1;
                middle = (head + end) / 2;
            } else if (nums[middle] > target) {
                end = middle - 1;
                middle = (head + end) / 2;
            } else {
                return middle;
            }

        }
        return -1;
    }


    public static void main(String[] args) {
        Solution704 solution = new Solution704();
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int target = 9;
        System.out.println(solution.search(nums, target));
    }
}
